/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software. 
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package com.ecarx.camera.utils;

import android.util.Log;

import com.ecarx.camera.BuildConfig;

public class LogUtils {
    private static final String TAG = "XCCamera[" + BuildConfig.VERSION_NAME + "]";
    private static final String TAG_SUB = "Camera";
    private static final boolean LOG_SWITCH = true;
    private static final boolean SHOW_CLASS = false;

    private static final int V = 0x01;
    private static final int D = 0x02;
    private static final int I = 0x04;
    private static final int W = 0x08;
    private static final int E = 0x10;
    private static final int A = 0x20;
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static void v(Object contents) {
        log(V, TAG_SUB, contents);
    }

    public static void v(String tag, Object... contents) {
        log(V, tag, contents);
    }

    public static void d(Object contents) {
        log(D, TAG_SUB, contents);
    }

    public static void d(String tag, Object... contents) {
        log(D, tag, contents);
    }

    public static void i(Object contents) {
        log(I, TAG_SUB, contents);
    }

    public static void i(String tag, Object... contents) {
        log(I, tag, contents);
    }

    public static void w(Object contents) {
        log(W, TAG_SUB, contents);
    }

    public static void w(String tag, Object... contents) {
        log(W, tag, contents);
    }

    public static void e(Object contents) {
        log(E, TAG_SUB, contents);
    }

    public static void e(String tag, Object... contents) {
        log(E, tag, contents);
    }

    public static void a(Object contents) {
        log(A, TAG_SUB, contents);
    }

    public static void a(String tag, Object... contents) {
        log(A, tag, contents);
    }

    private static void log(int type, String tag, Object... contents) {
        if (!LOG_SWITCH) return;
        final String msg = processContents(tag, contents);
        switch (type) {
            case V:
                Log.v(TAG, msg);
                break;
            case D:
                Log.d(TAG, msg);
                break;
            case I:
                Log.i(TAG, msg);
                break;
            case W:
                Log.w(TAG, msg);
                break;
            case E:
                Log.e(TAG, msg);
                break;
            case A:
                Log.wtf(TAG, msg);
                break;
        }

    }

    private static String processContents(String subTag, Object... contents) {
        final Thread curThread = Thread.currentThread();
        StackTraceElement targetElement = curThread.getStackTrace()[5];
        String className = targetElement.getClassName();
        String[] classNameInfo = className.split("\\.");
        if (classNameInfo.length > 0) {
            className = classNameInfo[classNameInfo.length - 1];
        }
        if (className.contains("$")) {
            className = className.split("\\$")[0];
        }
        //String head = String.format("[%s][%s][%s(%s.java:%d)] <%s>  ", BuildConfig.VERSION_NAME, curThread.getName(), targetElement.getMethodName(), className, targetElement.getLineNumber(), subTag);
        String head = String.format("%s[%s]:  ", subTag, curThread.getName());
        String foot = SHOW_CLASS ? String.format("  ==> %s(%s.java:%d)", targetElement.getMethodName(), className, targetElement.getLineNumber()) : "";
        String msg;
        if (contents != null) {
            if (contents.length == 1) {
                Object object = contents[0];
                msg = object == null ? "NULL" : object.toString();
            } else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0, len = contents.length; i < len; ++i) {
                    Object content = contents[i];
                    sb.append("args")
                            .append("[")
                            .append(i)
                            .append("]")
                            .append(" = ")
                            .append(content == null ? "NULL" : content.toString())
                            .append(LINE_SEPARATOR);
                }
                msg = sb.toString();
            }
        } else {
            msg = "log object is null";
        }
        return head + msg + foot;
    }
}
